//
//  ViewController.m
//  20作业汽车之家
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "ChoseCarModel.h"
#import "AllCarModel.h"
#import "CustomTableViewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

//@property (nonatomic,strong) UIScrollView *allScrollView;

@property (nonatomic,strong) NSMutableArray *indexArr;
@property (nonatomic,strong) NSMutableSet *indexSet;
@property (nonatomic,strong) AllCarModel *allCarModel;
@property (nonatomic,strong) NSMutableArray *allCarArr;
@property (nonatomic,strong) NSMutableArray *letterArr;

@property (nonatomic,strong) ChoseCarModel *choseModel;
@property (nonatomic,strong) NSMutableArray *choseModelArr;
@property (nonatomic,strong) NSMutableArray *nameArr;
@property (nonatomic,strong) NSMutableArray *hotNameArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    _allScrollView.contentOffset = CGPointMake(0, 100000000);
//    _allScrollView.showsHorizontalScrollIndicator = YES;
//    _allScrollView.pagingEnabled = YES;
//    _allScrollView.scrollEnabled = YES;
//    _allScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height - 44)];
////    [self.view addSubview:_allScrollView];
//    _allScrollView.delegate = self;
//    _allScrollView.backgroundColor = [UIColor greenColor];
    
//    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
////    tableView.backgroundColor = [UIColor redColor];
//    [self.view addSubview:tableView];
//    
//    tableView.delegate = self;
//    tableView.dataSource = self;
//    
//    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableCell"];
    
    
    
    [self handleTableData];
    
   
    
    
    NSString *hotBrandDataPath = [[NSBundle mainBundle] pathForResource:@"HotBrand" ofType:@"json"];
//     NSLog(@"%@",hotBrandDataPath);
    NSData *hotData = [NSData dataWithContentsOfFile:hotBrandDataPath];
//     NSLog(@"%@",hotData);
    NSDictionary *hotDic = [NSJSONSerialization JSONObjectWithData:hotData options:NSJSONReadingMutableContainers error:nil];
//     NSLog(@"%@",hotDic);
    NSArray *hotArr = hotDic[@"result"][@"list"];
    _hotNameArr = [NSMutableArray array];
//     NSLog(@"%@",hotArr);
    for (NSDictionary *dic in hotArr) {
        NSString *nameStr = dic[@"name"];
        [_hotNameArr addObject:nameStr];
    }
//     NSLog(@"%@",_hotNameArr);
    
    
    NSString *extensionDataPath = [[NSBundle mainBundle] pathForResource:@"Extension" ofType:@"json"];
//     NSLog(@"%@",extensionDataPath);
    NSData *extensionData = [NSData dataWithContentsOfFile:extensionDataPath];
//     NSLog(@"%@",extensionData);
    NSDictionary *extensionDic = [NSJSONSerialization JSONObjectWithData:extensionData options:NSJSONReadingMutableContainers error:nil];
//     NSLog(@"%@",extensionDic);
  NSDictionary *extensionMessageDic =  extensionDic[@"result"];
//     NSLog(@"%@",extensionMessageDic);
    NSArray *extensionArr = extensionMessageDic[@"list"];
//     NSLog(@"%@",extensionArr);
//    未解析完
    
    
    NSString *chooseDataPath = [[NSBundle mainBundle] pathForResource:@"ChoseCar" ofType:@"json"];
//     NSLog(@"%@",chooseDataPath);
    NSData *chooseData = [NSData dataWithContentsOfFile:chooseDataPath];
//     NSLog(@"%@",chooseData);
    NSDictionary *choseDic = [NSJSONSerialization JSONObjectWithData:chooseData options:NSJSONReadingMutableContainers error:nil];
//     NSLog(@"%@",choseDic);
    
    NSArray *chooseArr = choseDic[@"result"][@"metalist"];
    _choseModelArr = [NSMutableArray array];
//     NSLog(@"%@",chooseArr);
    for (NSDictionary *dic in chooseArr) {
        NSArray *listArr = dic[@"list"];
        for (NSDictionary *dic in listArr) {
            NSString *nameStr = dic[@"name"];
            NSString *value = (NSString *)dic[@"value"];
            _choseModel = [[ChoseCarModel alloc] initWithName:nameStr value:value];
            [_choseModelArr addObject:_choseModel];
        }
//         NSLog(@"%@",listArr);
        
    }
    
    
    for (ChoseCarModel *model in _choseModelArr) {
//        NSLog(@"%@",model.name);
    }
    
    
    
    
}

- (void)handleTableData
{
    NSString *allCardataPath = [[NSBundle mainBundle] pathForResource:@"AllCar" ofType:@"json"];
    //     NSLog(@"%@",dataPath);
    NSError *error = nil;
    NSData *jsonData = [NSData dataWithContentsOfFile:allCardataPath];
    //     NSLog(@"%@",jsonData);
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"%@",jsonDic);
    NSArray *brandlistArr = jsonDic[@"result"][@"brandlist"];
    _nameArr = [NSMutableArray array];
    _allCarArr = [NSMutableArray array];
    _letterArr = [NSMutableArray array];
    for (NSDictionary *dic in brandlistArr) {
        NSString *letter = dic[@"letter"];
        
        [_letterArr addObject:letter];
        NSArray *listArr = dic[@"list"];
        for (NSDictionary *dic in listArr) {
            NSString *nameStr = dic[@"name"];
            UIImage *image = dic[@"imgurl"];
            _allCarModel = [[AllCarModel alloc] initWithName:nameStr allCarImage:image];
            [_allCarArr addObject:_allCarModel];
            //            NSLog(@"%@",_allCarModel.allCarImage);
            //            [_nameArr addObject:nameStr];
        }
    }
    NSLog(@"%@",_letterArr);
    _indexArr = [NSMutableArray array];
    _indexSet = [NSMutableSet set];
    for (AllCarModel *model in _allCarArr) {
        NSMutableString *nameString = [NSMutableString stringWithString:model.name];
        Boolean nameStr = CFStringTransform((CFMutableStringRef)nameString, NULL, kCFStringTransformToLatin, false);
        //        if ([nameString uppercaseString]) {
        //            <#statements#>
        //        }
        // NSLog(@"%@",[nameString substringToIndex:1]);
        NSString *indexName = [nameString substringToIndex:1];
        [_indexArr addObject:indexName];
        //        [_indexSet addObject:indexName];
        
        
        //         NSLog(@"%@",nameString);
    }
//    NSLog(@"%@",_indexArr);
    //     NSLog(@"%@",_indexSet);

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell"];
    cell.textLabel.text = @"小子";
    return cell;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
